public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.myPow(2.0, Integer.MIN_VALUE));
        System.out.println(solution.myPow(2.1, 3));
        System.out.println(solution.myPow(2.0, -2));
        System.out.println(solution.myPow(2.0, 0));
    }
}

// 官方题解，快速幂递归法
class Solution {
    public double myPow(double x, int n) {
        if(n > 0) return quickMul(x, n);
        return 1.0 / quickMul(x, -(long)n);
    }

    double quickMul(double x, long n){
        if(n == 0){
            return 1.0;
        }
        double y = quickMul(x, n / 2);
        return (n & 1) == 0 ? y * y : y * y * x;
    }
}

// 官方题解，快速幂迭代法
class Solution1 {
    public double myPow(double x, int n) {
        if(n > 0) return quickMul(x, n);
        return 1.0 / quickMul(x, -(long)n);
    }

    double quickMul(double x, long n){
        double ans = 1.0;
        double contribute = x;
        while(n > 0){
            if((n & 1) != 0){
                ans *= contribute;
            }
            contribute *= contribute;
            n >>= 1;
        }
        return ans;
    }
}